package com.lora.scheduled;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.lora.service.EquipmentService;
import com.lora.web.LowerHairService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class CmdScheduled {

    private final static String COMMAND_KEY = "command";

    @Resource
    private EquipmentService equipmentService;

    @Resource
    private LowerHairService lowerHairService;

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    public void query1() {
        this.executeCommand("query1");
    }

    public void query2() {
        this.executeCommand("query2");
    }

    public void query3() {
        this.executeCommand("query3");
    }

    public void query4() {
        this.executeCommand("query4");
    }

    public void query5() {
        this.executeCommand("query5");
    }

    public void query6() {
        this.executeCommand("query6");
    }

    public void query7() {
        this.executeCommand("query7");
    }

    public void query8() {
        this.executeCommand("query8");
    }

    public void query9() {
        this.executeCommand("query9");
    }

    public void query10() {
        this.executeCommand("query10");
    }

    public void query11() {
        this.executeCommand("query11");
    }

    public void query12() {
        this.executeCommand("query12");
    }

    public void query13() {
        this.executeCommand("query13");
    }

    public void query14() {
        this.executeCommand("query14");
    }

    public void query15() {
        this.executeCommand("query15");
    }

    public void query16() {
        this.executeCommand("query16");
    }

    public void query17() {
        this.executeCommand("query17");
    }

    public void query18() {
        this.executeCommand("query18");
    }

    public void query19() {
        this.executeCommand("query19");
    }

    public void query20() {
        this.executeCommand("query20");
    }

    public void circularProcess() {
        String command = redisTemplate.opsForList().rightPop(COMMAND_KEY);
        if(StringUtils.isNotBlank(command)) {
            JSONObject commandObj = JSONObject.parseObject(command);
            String eid = commandObj.getString("eid");
            String cmd = commandObj.getString("cmd");
            lowerHairService.lowerHair(eid, cmd);
        }
    }

    private void executeCommand(String method) {
        String[] parameter = this.loadQueryCmd(method);
        // log.info("execute method: {}, parameter is {}", method, parameter);
        for(String cmd : parameter[0].split(",")) {
            JSONObject command = new JSONObject();
            command.put("eid", parameter[1]);
            command.put("cmd", cmd);
            redisTemplate.opsForList().leftPush(COMMAND_KEY, command.toJSONString());
        }
    }

    private String[] loadQueryCmd(String method) {
        String parameter = null;
        String eid = null;
        Map<String, Object> map = Maps.newHashMap();
        map.put("method", method);
        List<Map<String, Object>> query = equipmentService.query(map);
        if(!query.isEmpty()) {
            parameter = (String) query.get(0).get("parameter");
            eid = (String) query.get(0).get("eid");
        }
        return StringUtils.isNotBlank(parameter) ? new String[] {parameter, eid} : null;
    }

}
